package com.ddwl.user.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.UserEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.schema.bo.user.UserAgentQueryBo;
import com.ddwl.schema.bo.user.UserSearchBo;
import com.ddwl.schema.vo.order.StatisticsUserVo;
import com.ddwl.schema.vo.user.UserVo;
import com.ddwl.user.constant.CompanyAccountEnum;
import com.ddwl.user.dao.mapper.UserMapper;
import com.ddwl.user.dao.model.User;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * 类UserDao.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-06-22 16:46.
 */
@Repository
public class UserDao extends TkDao<UserMapper, User> {

    @Resource
    private UserMapper userMapper;

    public User queryByMobile(String mobile) {
        User user = new User();
        user.setMobile(mobile);
        return selectOne(user);
    }

    public void updateNameAndBir(User user) {
        userMapper.updateNameAndBir(user);
    }


    public User queryByInviteCode(String inviteCode) {
        User user = new User();
        user.setInviteCode(inviteCode);
        return selectOne(user);
    }

    public User queryByRegion(String region) {
        User user = new User();
        user.setAgentRegion(region);
        user.setType(UserEnum.UserTypeEnum.REGION.getValue());
        return selectOne(user);
    }


    public void removeUserDist(String userId) {
        baseMapper.removeUserDist(userId);
    }

    /**
     * 查询用户的下级
     *
     * @param userId
     * @return
     */
    public List<User> queryUserLower(String userId) {
        Example example = getExample();
        example.and().orEqualTo("immediateSuperior", userId)
                .orEqualTo("indirectSuperior", userId);
        return baseMapper.selectByExample(example);
    }


    public List<User> queryPage(UserSearchBo bo) {
        Example example = getExample();
        example.and().orLike("nickname", MySqlUtil.allLike(bo.getKeyword()))
                .orLike("realname", MySqlUtil.allLike(bo.getKeyword()))
                .orLike("mobile", MySqlUtil.allLike(bo.getKeyword()))
                .andEqualTo("sex", bo.getSex())
                .andEqualTo("id", bo.getId())
                .andIn("id", bo.getIds())
                .andEqualTo("product", bo.getProduct())
                .andEqualTo("type", bo.getType())
                .andEqualTo("userLevelId", bo.getUserLevelId())
                .andEqualTo("inviteCode", bo.getInviteCode())
                .andEqualTo("companyAccount", bo.getCompanyAccount())
                .andGreaterThanOrEqualTo("gmtCreated", bo.getBeginDate())
                .andLessThan("gmtCreated", bo.getEndDate())
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return baseMapper.selectByExample(example);
    }

    public List<StatisticsUserVo> queryUserCount(UserSearchBo bo) {
        return baseMapper.queryUserCount(bo);
    }

    /**
     * 修改上级用户昵称（包括直接上级和间接上级）
     *
     * @param id
     * @param nickname
     */
    public void updateSuperiorNickname(String id, String nickname) {
        baseMapper.updateImmediateSuperiorNickname(id, nickname);
        baseMapper.updateIndirectSuperiorName(id, nickname);
    }

    public User queryCompanyAccount() {
        User user = new User();
        user.setCompanyAccount(CompanyAccountEnum.COMPANY_ACCOUNT.getValue());
        return selectOne(user);
    }

    public void upToReionBoss(User entity) {
        userMapper.upToReionBoss(entity);
    }

    public List<User> queryDirectSubUser(String id) {
        User user = new User();
        user.setImmediateSuperior(id);
        return select(user);
    }

    public void updateSuperiorField(User directSub) {
        userMapper.updateSuperiorField(directSub);
    }

    public void updateTopAgent(List<String> userIds, String topAgent) {
        User user = new User();
        user.setGmtModified(Instant.now());
        user.setTopAgent(topAgent);
        Example example = getExample();
        example.and().andIn("id", userIds);
        userMapper.updateByExampleSelective(user, example);

    }

    public Boolean isNewUser(String userId, Instant startTime, Instant endTime) {
        Example example = getExample();
        example.and().andEqualTo("id", userId)
                .andGreaterThanOrEqualTo("gmtCreated", startTime)
                .andLessThanOrEqualTo("gmtCreated", endTime);
        User entity = userMapper.selectOneByExample(example);
        if (null != entity) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public void changeUserLevelId(Map<String, String> map) {
        userMapper.changeUserLevelId(map);
    }

    public UserVo getByOpenId(String openId, String pk) {
        return userMapper.getByOpenId(openId, pk);
    }

    public User getByUnionId(String unionId) {
        User user = new User();
        user.setUnionId(unionId);
        return selectOne(user);
    }

    public User getByByteUnionId(String unionId) {
        User user = new User();
        user.setByteUnionId(unionId);
        return selectOne(user);
    }

    public List<User> selectSubAgentUser(UserAgentQueryBo bo) {
        Example example = getExample();
        example.and().andEqualTo("immediateSuperior", bo.getUserId())
                .andNotEqualTo("profitLevel", UserEnum.ProfitLevelEnum.NORMAL.getLevel());
        example.and().andLike("mobile", MySqlUtil.allLike(bo.getKeyWords()))
                .orLike("nickname", MySqlUtil.allLike(bo.getKeyWords()))
                .orLike("realname", MySqlUtil.allLike(bo.getKeyWords()));
        return userMapper.selectByExample(example);
    }

    public User queryByKeyword(String keyword) {
        Example example = getExample();
        example.and().orEqualTo("mobile", keyword).orEqualTo("inviteCode", keyword);
        return userMapper.selectOneByExample(example);
    }

}
